package com.angel.controller;

import com.angel.bean.NoticeMater;
import com.angel.exception.StatusException;
import com.angel.service.NoticeMaterService;
import com.angel.service.OtherlogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/noticeMaterController")
public class NoticeMaterController {

    @Autowired
    private NoticeMaterService noticeMaterService;

    @Autowired
    private OtherlogService otherlogService;

    /**
     * 删除
     *
     * @param noticeMater
     * @return
     */
    @RequestMapping("/delNoticeMater")
    @ResponseBody
    public Object delNoticeMater(NoticeMater noticeMater) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        noticeMater.setSate(0);
        int result = noticeMaterService.update(noticeMater);
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 添加
     *
     * @param noticeMater
     * @return
     */
    @RequestMapping("/addNoticeMater")
    @ResponseBody
    public Object addNoticeMater(NoticeMater noticeMater) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        noticeMater.setSate(1);
        int result = noticeMaterService.insert(noticeMater);
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 更新记录
     *
     * @param noticeMater
     * @return
     */
    @RequestMapping("/updateNoticeMater")
    @ResponseBody
    public Object updateNoticeMater(NoticeMater noticeMater) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = noticeMaterService.update(noticeMater);
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 查询单个记录
     *
     * @param noticeMater
     * @return
     */
    @RequestMapping("/findOneNoticeMater")
    @ResponseBody
    public Object findOneNoticeMater(NoticeMater noticeMater) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        noticeMater.setSate(1);
        NoticeMater notice = noticeMaterService.findOneNoticeMater(noticeMater);
        if (notice != null) {
            map.put("notice", notice);
        }

        return map;
    }

    /**
     * 条件查询
     *
     * @param model
     * @return
     */
    @RequestMapping("/findNoticeMater")
    @ExceptionHandler(value = StatusException.class)
    public String findNoticeMater(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {

        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 5);
        NoticeMater noticeMater = new NoticeMater();
        noticeMater.setSate(1);

        List<NoticeMater> noticeMaterList = noticeMaterService.findNoticeMater(noticeMater);

        PageInfo pageInfo = new PageInfo(noticeMaterList);
        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "noticeMaterEntry";
    }
}
